package com.itheima.web;

import com.itheima.service.UserService;
import com.itheima.tanhua.exception.ConsumerException;
import com.itheima.tanhua.pojo.db.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author 芝士雪豹
 * @create 2022/9/18/20:52
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;


    @PostMapping("login")
    public ResponseEntity login(@RequestBody Map<String, String> map) {
        boolean flag = userService.senMsg(map.get("phone"));
        if (!flag) {
            //服务器内部错误 INTERNAL_SERVER_ERROR=500
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("验证码还没生效结束!");
        }
        return ResponseEntity.ok(null); //200 返回成功
    }

    /**
     * 手机号验证码登录
     *
     * @param pram
     * @return
     */
    @PostMapping("loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String, String> pram)  {
        Map map = userService.loginVerification(pram);
        return ResponseEntity.ok(map);
    }

    /**
     * 新用户完善资料
     * @return
     */
    @PostMapping("loginReginfo")
    public ResponseEntity loginReginfo(@RequestHeader("Authorization")String token,@RequestBody UserInfo userInfo) {
        userService.addUserInfo(token,userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 用户补充头像
     * @return
     */
    @PostMapping("loginReginfo/head")
    public ResponseEntity head(@RequestHeader("Authorization") String token, MultipartFile headPhoto) throws ConsumerException {
        String url = userService.upload(token,headPhoto);
        userService.addUserAvator(url, token);
        return ResponseEntity.ok(null);
    }

}
